﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Net;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Extensibility.Dom;
using Galaktika.BI.Silverlight.ClientApp.Items;
using Galaktika.BI.Silverlight.ClientApp.Items.Toolbar;
using Galaktika.BI.Runtime.Services;
using System.ComponentModel;

namespace Galaktika.BI.Silverlight.ClientApp
{
    public class MainToolbarHolder
    {
        private class UserToolbarEntry
        {
            public UserToolbarEntry(BrickUserToolbar toolbar)
            {
                this.UserToolbar = toolbar;
                this.Container = new StackPanel() { Orientation = Orientation.Horizontal };
            }

            public readonly BrickUserToolbar UserToolbar;
            public readonly StackPanel Container;
        }

        private StackPanel m_MainToolbar;
        private Dictionary<string, UserToolbarEntry> m_Storage;
        public MainToolbarHolder(StackPanel mainToolbar)
        {
            if (mainToolbar == null)
            {
                throw new ArgumentNullException("mainToolbar");
            }

            m_MainToolbar = mainToolbar;
            m_Storage = new Dictionary<string, UserToolbarEntry>();
        }

        public void AppendToolbar(BrickUserToolbar toolbar)
        {
            var entry = new UserToolbarEntry(toolbar);
            m_Storage.Add(toolbar.Name, entry);
            entry.Container.Name = toolbar.Name;
            m_MainToolbar.Children.Add(entry.Container);
            this.BuildContainer(entry);

            m_MainToolbar.Margin = new Thickness(4.0, 0.0, 4.0, 0.0);
        }

        public void RemoveToolbar(BrickUserToolbar toolbar)
        {
            var entry = m_Storage[toolbar.Name];
            m_Storage.Remove(toolbar.Name);
            m_MainToolbar.Children.Remove(entry.Container);
            if (m_MainToolbar.Children.Count == 0)
            {
                m_MainToolbar.Margin = new Thickness(0.0);
            }
        }

        public bool IsContains(BrickUserToolbar toolbar)
        {
            return m_Storage.ContainsKey(toolbar.Name);
        }

        public IEnumerable<BrickUserToolbar> GetCurrentToolbars()
        {
            var res = new List<BrickUserToolbar>(m_Storage.Count);

            foreach (var v in m_Storage.Values)
            {
                res.Add(v.UserToolbar);
            }
            return res.AsReadOnly();
        }

        public void SetBindingContext(IBindingContext context)
        {
            foreach (var store in m_Storage.Values)
            {
                foreach (var child in store.Container.Children)
                {
                    if (child is IEditableObject)
                    {
                        ((IEditableObject)child).BeginEdit();
                    }
                    if (child is IBindableObject)
                    {
                        ((IBindableObject)child).Context = context;
                    }
                    if (child is IEditableObject)
                    {
                        ((IEditableObject)child).EndEdit();
                    }
                }
            }
        }

        private void BuildContainer(UserToolbarEntry entry)
        {
            foreach (var item in entry.UserToolbar.ToolbarItems)
            {
                var fe = ToolbarItemFactory.CreateItem(item);
                if (fe == null) continue;
                if (ToolbarItemFactory.IsBeginGroup(item))
                {
                    entry.Container.Children.Add(new MenuSplitter() { Margin = new Thickness(4.0) });
                }
                entry.Container.Children.Add(fe);
            }
        }
    }
}
